//
//  ZYMineHeadView.swift
//  WeiBoAPP
//
//  Created by ios001 on 2022/11/28.
//  Copyright © 2022 zxf. All rights reserved.
//

import UIKit

class ZYMineHeadView: UIView {

    typealias HomeClickBlock = ()->Void
    
    var frofilePageActionBlock:HomeClickBlock?
    var loginActionBlock:HomeClickBlock?
    var loginButton:UIButton?
    
    override init(frame: CGRect) {
        super .init(frame: frame);
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    private func setupUI() {
        
        // 跳转
        let btn:UIButton = UIButton(type: UIButton.ButtonType.custom)
        btn.frame = CGRect(x: 20, y: 10, width: 100, height: 30);
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        btn.setTitle("SQLIT数据库", for: UIControl.State.normal)
        btn.setTitleColor(UIColor.red, for: UIControl.State.normal)
        btn.addTarget(self, action: #selector(jumpToFrofilePageAction), for: UIControl.Event.touchUpInside)
        addSubview(btn)
        
        
        let headImageView:UIImageView = UIImageView.init()
        headImageView.layer.borderColor = UIColor.red.cgColor
        headImageView.layer.borderWidth = 1
        headImageView.layer.cornerRadius = 35.0
        headImageView.layer.masksToBounds = true
        addSubview(headImageView)
        headImageView.snp.makeConstraints { make in
            make.width.height.equalTo(70)
            make.centerX.equalTo(self)
            make.top.equalTo(20)
        }
        
        // 登录
        let loginBtn:UIButton = UIButton(type: UIButton.ButtonType.custom)
//        loginBtn.frame = CGRect(x: 0, y: 0, width: 50, height: 50);
//        loginBtn.center = CGPoint(x: headerView.frame.size.width * 0.5, y: 25)
//        loginBtn.backgroundColor = UIColor.green
        loginBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        loginBtn.setTitle("去登录", for: UIControl.State.normal)
        loginBtn.setTitleColor(UIColor.black, for: UIControl.State.normal)
        loginBtn.layer.borderWidth = 1
        loginBtn.layer.borderColor = UIColor.green.cgColor
        loginBtn.layer.cornerRadius = 5
        loginBtn.addTarget(self, action: #selector(loginBtnAction), for: UIControl.Event.touchUpInside)
        addSubview(loginBtn);
        self.loginButton = loginBtn
        
        loginBtn.snp.makeConstraints { make in
            make.height.equalTo(40)
            make.width.equalTo(170)
            make.top.equalTo(headImageView.snp_bottom).offset(10)
            make.centerX.equalToSuperview()
        }
    }
    
    @objc private func jumpToFrofilePageAction () {
        
        self.frofilePageActionBlock?()

    }
    
    @objc private func loginBtnAction () {
        
        if UserDataConfig.shared.isLogin ?? false {
            return
        }
        
        self.loginActionBlock?()
       
    }
    
    func refreshHeadView() {
        if UserDataConfig.shared.isLogin == true {
            let userName = UserDataConfig.shared.userName
            loginButton?.setTitle(userName, for: .normal)
        }else{
            loginButton?.setTitle("去登录", for: .normal)
        }
        
        
    }
    

    
}
